package com.xydtech.ecard.dept.controller;

import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.common.model.ResponseModel;
import com.xydtech.ecard.dept.domain.EcardApiSysDept;
import com.xydtech.ecard.dept.service.IEcardApiSysDeptService;
import com.xydtech.ecard.ecard.controller.EcardApiBaseController;
import com.xydtech.ecard.user.domain.EcardApiUserInfo;
import com.xydtech.ecard.user.service.IEcardApiUserService;
import com.xydtech.system.service.ISysConfigService;
import com.xydtech.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 部门Controller
 *
 * @author Daniel
 * @date 2021-09-24
 */
@Controller
@Api(tags = "电子名片-部门管理")
@RequestMapping("/ecardSaasApi/dept")
public class EcardApiSysDeptController extends EcardApiBaseController {
    @Autowired
    private IEcardApiSysDeptService ecardApiSysDeptService;
    @Autowired
    private IEcardApiUserService ecardApiUserService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 电子名片-部门管理-获取当前法人下所有机构
     * @return
     */
    @ApiOperation(value = "电子名片-部门管理-获取当前法人下所有机构", notes = "电子名片-部门管理-获取当前法人下所有机构")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiSysDept.class)})
    @GetMapping("/getSameLevelDepts")
    @ResponseBody
    public ResponseModel getSameLevelDepts() {
        String salt = getSalt();//盐值
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);

        //法人机构信息
        SysDept secondLevelDept = deptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
        //获取法人机构的ID
        Long legalManDeptId = secondLevelDept.getDeptId();

        List<EcardApiSysDept> ecardApiSysDeptList = ecardApiSysDeptService.getSameLevelDepts(legalManDeptId);
        return ResponseModel.ok(ecardApiSysDeptList);
    }

    /**
     * 电子名片-部门管理-根据参数配置展示法人下指定类型的机构
     * @return
     */
    @ApiOperation(value = "电子名片-部门管理-根据参数配置展示法人下指定类型的机构", notes = "电子名片-部门管理-根据参数配置展示法人下指定类型的机构")
    @ApiResponses({@ApiResponse(code = 0, message = "OK", response = EcardApiSysDept.class)})
    @GetMapping("/getDeptsByConfig")
    @ResponseBody
    public ResponseModel getDeptsByConfig() {
        String salt = getSalt();//盐值
        EcardApiUserInfo userInfo = ecardApiUserService.getUserInfo(getCurrentUserId(), salt);
        //法人机构信息
        SysDept secondLevelDept = deptService.selectSecondLevelDeptByDeptId(userInfo.getDeptId());
        //获取法人机构的ID
        Long legalManDeptId = secondLevelDept.getDeptId();
        //查询商户业务统计口径配置
        //0：查询各分支机构（支行、分理处）业务数据，1：查询各分支机构及部室数据
        String ecardMerchantIsdept = sysConfigService.selectConfigByKey("ecard_merchant_isdept", legalManDeptId);
        List<EcardApiSysDept> ecardApiSysDeptList = ecardApiSysDeptService.getDeptsByConfig(legalManDeptId, ecardMerchantIsdept);
        return ResponseModel.ok(ecardApiSysDeptList);
    }
}
